import platform
import os
import requests
import redis
from pymongo import MongoClient

with open("../config.py", 'r', encoding="utf-8") as f:
    exec(f.read())


def get_redis(redisinfo):
    pool = redis.ConnectionPool(host=redisinfo["ip"], port=redisinfo["port"], password=redisinfo["password"],
                                db=redisinfo["db"], decode_responses=True)
    return redis.Redis(connection_pool=pool, decode_responses=True)


def mongo_info():
    mongo_ip = mongoinfo['ip']
    mongo_port = mongoinfo['port']
    mongo_client = MongoClient(host=mongo_ip, port=mongo_port)
    try:
        dbname = mongo_client.list_database_names()
        collection_name = None
        config_name = None
        if "guidang4" in dbname:
            collection_name = mongo_client.guidang4.list_collection_names()

        if "guidang4config" in dbname:
            config_name = mongo_client.guidang4.list_collection_names()

        mongo_info = {
            "dbname": dbname,
            "guidang4": collection_name,
            "guidang4config": config_name
        }
    except Exception:
        mongo_info = {
            "mongo": "connect error"
        }
    return mongo_info


def redis_info():
    redis0 = get_redis(redisinfo0)  # 1vn
    redis1 = get_redis(redisinfo1)  # log
    redis2 = get_redis(redisinfo2)  # acvs_acvs
    redis3 = get_redis(redisinfo3)  # 建档的face_acvs
    redis4 = get_redis(redisinfo4)  # face_acvs
    redis5 = get_redis(redisinfo5)  # acvs_face
    redis6 = get_redis(redisinfo6)  # card_acvs
    redis7 = get_redis(redisinfo7)  # acvs_card
    redis8 = get_redis(redisinfo8)  # card_face
    redis9 = get_redis(redisinfo9)  # face_card
    redis10 = get_redis(redisinfo10)  # delete_card
    redis11 = get_redis(redisinfo11)  # del_acvs_acvs
    redis12 = get_redis(redisinfo12)  # del_face_acvs
    redis13 = get_redis(redisinfo13)  #
    redis14 = get_redis(redisinfo14)  #
    redis15 = get_redis(redisinfo15)  # allimg
    try:
        redis_info = {
            "db0": len(redis0.keys()),
            "db1": len(redis1.keys()),
            "db2": len(redis2.keys()),
            "db3": len(redis3.keys()),
            "db4": len(redis4.keys()),
            "db5": len(redis5.keys()),
            "db6": len(redis6.keys()),
            "db7": len(redis7.keys()),
            "db8": len(redis8.keys()),
            "db9": len(redis9.keys()),
            "db10": len(redis10.keys()),
            "db11": len(redis11.keys()),
            "db12": len(redis12.keys()),
            "db13": len(redis13.keys()),
            "db14": len(redis14.keys()),
            "db15": len(redis15.keys())
        }
    except Exception:
        redis_info = {
            "redis": "connect error"
        }
    return redis_info


def get_process():
    process_dic = {
        "RUN1": [],
        "RUN2": [],
        "RUN3": [],
        "RUN4": [],
        "redis": [],
        "mongodb": [],
        "redistop": [],
        "mongotop": []
    }
    if platform.system() == 'Linux':
        content = os.popen('ps -ef').read()
        for line in content.splitlines():
            if "RUN1" in line:
                process_dic.get("RUN1").append(line)
            if "RUN2" in line:
                process_dic.get("RUN2").append(line)
            if "RUN3" in line:
                process_dic.get("RUN3").append(line)
            if "RUN4" in line:
                process_dic.get("RUN4").append(line)
            if "redis" in line:
                process_dic.get("redis").append(line)
            if "mongod" in line:
                process_dic.get("mongodb").append(line)
        try:
            content2 = os.popen("bash ./mongo_top.sh").read()
            for line2 in content2.splitlines():
                process_dic.get("mongotop").append(line2)
        except Exception as e:
            print(e)
        try:
            content3 = os.popen("bash ./redis_top.sh").read()
            for line3 in content3.splitlines():
                process_dic.get("redistop").append(line3)
        except Exception as e:
            print(e)

        try:
            RUN1_test = requests.get('http://127.0.0.1:61002/test')
            process_dic.get("RUN1").append("RUN1请求61002：%s" % RUN1_test.text)
        except Exception:
            process_dic.get("RUN1").append("RUN1请求61002：失败")

        redisimg = {"redis_img": redis_info()}
        mongoimg = {"mongo_img": mongo_info()}
        process_dic.update(redisimg)
        process_dic.update(mongoimg)

    return process_dic
